package com.ttmusic.fragments.maintab;

import java.util.List;

import com.ttmusic.activitys.MainActivity;
import com.ttmusic.activitys.R;
import com.ttmusic.core.MusicManager;
import com.ttmusic.dao.MusicInfoDao;
import com.ttmusic.entity.AbstractMusic;
import com.ttmusic.entity.MusicInfo;
import com.ttmusic.entity.MusicList;
import com.ttmusic.view.adapters.MyLoveMusicAdapter;
import com.ttmusic.view.listeners.OnItemClickListener;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 歌单的歌曲列表
 * @author Administrator
 *
 */
public class MusicOfListFragment extends ListFragment implements OnClickListener {
	
	private Context mContext;
	private MusicInfoDao musicInfoDao;
	private LayoutInflater inflater;
	private View view;
	private ListView listview_mylove;
	private MyLoveMusicAdapter myLoveMusicAdapter;
	//当前歌单的歌曲列表
	private List<AbstractMusic> myloveMusicInfos;
	//歌单
	private MusicList musicList;
	private TextView playAll_this_list;
	public static boolean isCreated = false;
	
	public MusicOfListFragment(Context mContext, MusicList musicList) {
		this.mContext = mContext;
		this.musicList = musicList;
		this.musicInfoDao = MusicInfoDao.getInstance();
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		this.inflater = inflater;
		view = inflater.inflate(R.layout.fragment_music_of_musiclist, container, false);
		return view;
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		listview_mylove = this.getListView();
		playAll_this_list = (TextView)view.findViewById(R.id.playAll_this_list);
		myloveMusicInfos = musicInfoDao.findAllByListId(this.musicList.getListId());
		myLoveMusicAdapter = new MyLoveMusicAdapter(mContext, myloveMusicInfos);
		
		playAll_this_list.setText("播放全部(共" + myloveMusicInfos.size() + "首)");
		playAll_this_list.setOnClickListener(this);
		myLoveMusicAdapter.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(int position) {
				if(MusicManager.getPlayingList() != myloveMusicInfos) {
					//播放音乐时判断当前点击的音乐所在list是不是当前播放列表  若不是，则切换播放列表为当前fragment的musicInfoList
			        MusicManager.setPlayingList(myloveMusicInfos);
				}
				if(position != MusicManager.getNowPlayingIndex()) {
					//准备和播放
					MainActivity.adapterCurrentPlay = myLoveMusicAdapter;
			        MainActivity.mManager.prepareAndPlay(position);
				}
			}
		});
		listview_mylove.setAdapter(myLoveMusicAdapter);
		MainActivity.adapterCurrentShow = myLoveMusicAdapter;
		isCreated = true;
	}
	
	/**
	 * 本fragment显示和隐藏监听
	 */
	@Override
	public void onHiddenChanged(boolean ishidden) {
		if(!ishidden) {
			myloveMusicInfos = musicInfoDao.findAllByListId(this.musicList.getListId());
			playAll_this_list.setText("播放全部(共" + myloveMusicInfos.size() + "首)");
			myLoveMusicAdapter.setList(myloveMusicInfos);
			MainActivity.adapterCurrentShow = myLoveMusicAdapter;
			if(MainActivity.adapterCurrentPlay == myLoveMusicAdapter) {
				//切换到当前fragment时 自动滚动到当前播放的音乐位置处
				//带滚动效果 滚动到指定索引处 由于添加了headerView 所以要+1
				listview_mylove.smoothScrollToPosition(MusicManager.getNowPlayingIndex()+1);
			}
			myLoveMusicAdapter.notifyDataSetChanged();
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.playAll_this_list:
				if(null != myloveMusicInfos && myloveMusicInfos.size() > 0) {
					if(MusicManager.getPlayingList() != myloveMusicInfos) {
						//播放音乐时判断当前点击的音乐所在list是不是当前播放列表  若不是，则切换播放列表为当前fragment的musicInfoList
				        MusicManager.setPlayingList(myloveMusicInfos);
				        MainActivity.adapterCurrentPlay = myLoveMusicAdapter;
					}
					MainActivity.mManager.prepareAndPlay(0);
				}
				break;
			default:
				break;
		}
	}

	public void setMusicList(MusicList musicList) {
		this.musicList = musicList;
	}

}
